<?php 
include('ConnectBdd.php');
?>

<center style="background: -webkit-linear-gradient(top,yellow,orange); border-radius:0 0 10px 10px;">
    <br>
    Sélèctionnez une poule: 
        <?php if (isset($_POST['choixPoule']))       
               {             
           ?>   
         <select id="choixPoule" name="choixPoule" class="log" style="width: 190px; height:30px;" onchange="AfficherClassement()">
              <?php
                                    // On récupère les poules existantes
                              $reqP="Select p.Id_Poule, Nom_Poule 
                                     From volley_poule p, volley_rattacher r, volley_tournois t 
                                     Where p.Id_Poule = r.Id_Poule AND
                                     r.Num_Tournois = t.Num_Tournois AND 
                                     InscriptionFermee=0 AND 
                                     Annees_Tournois= ".date("Y");
                                   $result = $connexion->query($reqP);
                              while ($row=$result->fetch(PDO::FETCH_ASSOC))
                        {
                                   if($row['Id_Poule']==$_POST['choixPoule']){
                                        echo "<option value = '".$row['Id_Poule']."' selected>".$row['Nom_Poule']."</option>";
                                   }else{
                                        echo "<option value = '".$row['Id_Poule']."'>".$row['Nom_Poule']."</option>";
                                   }
                        }
                ?>
         </select>
           <?php } else { ?>
         <select id="choixPoule" name="choixPoule" class="log" style="width: 190px; height:30px;" onchange="AfficherClassement()">
              <option value="1">Choisir</option>
              <?php
                                    // On récupère les poules existantes
                              $reqP="Select p.Id_Poule, Nom_Poule 
                                     From volley_poule p, volley_rattacher r, volley_tournois t 
                                     Where p.Id_Poule = r.Id_Poule AND
                                     r.Num_Tournois = t.Num_Tournois AND 
                                     InscriptionFermee=0 AND 
                                     Annees_Tournois= ".date("Y");
                              $result = $connexion->query($reqP);
                              while ($row=$result->fetch(PDO::FETCH_ASSOC))
                        {
                                echo "<option value = '".$row['Id_Poule']."'>".$row['Nom_Poule']."</option>";
                        }
                ?>
         </select>
          <?php } ?>          
    <br>
    <br>
    <?php if(isset($_POST['choixPoule'])){ 

		//initialisation du tab de points
		$req="SELECT Id_Equipe
                   FROM volley_inscrire i
                   WHERE Id_Poule='".$_POST['choixPoule']."'";
          $result = $connexion->query($req);
		  $arrayPoints = array();
		  
		while ($row=$result->fetch(PDO::FETCH_ASSOC))
        {
			$arrayPoints[$row['Id_Equipe']] = 0;
			$arrayDiffSets[$row['Id_Equipe']] = 0;
			$arrayDiffPoints[$row['Id_Equipe']] = 0;
			$arrayMatchsJoues[$row['Id_Equipe']] = 0;
			$arrayMatchsGagnes[$row['Id_Equipe']] = 0;
			$arrayMatchsPerdu[$row['Id_Equipe']] = 0;
			$arrayMatchs3_0[$row['Id_Equipe']] = 0;
			$arrayMatchs3_1[$row['Id_Equipe']] = 0;
			$arrayMatchs3_2[$row['Id_Equipe']] = 0;
			$arrayMatchs2_3[$row['Id_Equipe']] = 0;
			$arrayMatchs1_3[$row['Id_Equipe']] = 0;
			$arrayMatchs0_3[$row['Id_Equipe']] = 0;
			$arraySetP[$row['Id_Equipe']] = 0;
			$arraySetC[$row['Id_Equipe']] = 0;
		}
	

	
        //On récupére tous les matchs de la poule sélèctionnée
        $req="SELECT dom.Id_Equipe id1, ext.Id_Equipe id2,dom.Libelle_Equipe eq1,Score_Set1_Dom,Score_Set1_Ext,Score_Set2_Dom,Score_Set2_Ext,Score_Set3_Dom,Score_Set3_Ext,Score_Set4_Dom,Score_Set4_Ext,Score_Set5_Dom,Score_Set5_Ext
                   FROM volley_equipe dom,volley_equipe ext, volley_match, volley_inscrire i
                   WHERE dom.Id_Equipe=Id_Equipe_dom
                   and ext.Id_Equipe=Id_Equipe_ext
				   and i.Id_Equipe=dom.Id_Equipe
                   and Id_Poule='".$_POST['choixPoule']."'";
        $result = $connexion->query($req);
		

		//parcours de tous les matchs de la poule  
		while ($row=$result->fetch(PDO::FETCH_ASSOC))
        {
			//Pour chaque match
			$nbSetEq1=0;
			$nbSetEq2=0;
			//Compter les points (3/0 ou 3/1 vaut 3 points, 3/2 vaut 2 points, 2/3 vaut 1 point, 0 sinon)
			//Premier set
			if($row['Score_Set1_Dom'] >$row['Score_Set1_Ext']){
				$nbSetEq1 +=1;
			}else if($row['Score_Set1_Dom'] < $row['Score_Set1_Ext']){
				$nbSetEq2 +=1;
			}
			//Second set
			if($row['Score_Set2_Dom']>$row['Score_Set2_Ext']){
				$nbSetEq1 +=1;
			}else if($row['Score_Set2_Dom'] < $row['Score_Set2_Ext']){
				$nbSetEq2 +=1;
			}
			//troisieme set
			if($row['Score_Set3_Dom'] >$row['Score_Set3_Ext']){
				$nbSetEq1 +=1;
			}else if($row['Score_Set3_Dom'] < $row['Score_Set3_Ext']){
				$nbSetEq2 +=1;
			}
			//Quatrieme set
			if($row['Score_Set4_Dom'] >$row['Score_Set4_Ext']){
				$nbSetEq1 +=1;
			}else if($row['Score_Set4_Dom'] < $row['Score_Set4_Ext']){
				$nbSetEq2 +=1;
			}
			//Cinquieme set
			if($row['Score_Set5_Dom'] >$row['Score_Set5_Ext']){
				$nbSetEq1 +=1;
			}else if($row['Score_Set5_Dom'] < $row['Score_Set5_Ext']){
				$nbSetEq2 +=1;
			}
			
			//Un match de plus joue pour eq1 et eq2
			$arrayMatchsJoues[$row['id1']] += 1;
			$arrayMatchsJoues[$row['id2']] += 1;
			
			//On va gerer uniquement pour l'instant le jeu en 5 sets (3 sets gagnant)
			if($nbSetEq1==3 && $nbSetEq2<=1 ){
				$arrayPoints[$row['id1']] += 3;
				$arrayMatchsGagnes[$row['id1']] += 1;
                                $arrayMatchsPerdu[$row['id2']] += 1;
                                $arrayMatchs3_0[$row['id1']] += 1;
                                $arrayMatchs3_1[$row['id1']] += 1;
                                $arrayMatchs0_3[$row['id2']] += 1;
                                $arrayMatchs1_3[$row['id2']] += 1;
                                
			}else if($nbSetEq1==3 && $nbSetEq2==2){
				$arrayPoints[$row['id1']] += 2;
				$arrayPoints[$row['id2']] += 1;
				$arrayMatchsGagnes[$row['id1']] += 1;
                                $arrayMatchsPerdu[$row['id2']] += 1;
                                $arrayMatchs3_2[$row['id1']] += 1;
                                 $arrayMatchs2_3[$row['id2']] += 1;
                                
			}else if($nbSetEq1==2 && $nbSetEq2==3){
				$arrayPoints[$row['id1']] += 1;
				$arrayPoints[$row['id2']] += 2;
				$arrayMatchsGagnes[$row['id2']] += 1;
                                $arrayMatchsPerdu[$row['id1']] += 1;
                                $arrayMatchs2_3[$row['id1']] += 1;
                                $arrayMatchs3_2[$row['id2']] += 1;
                                
			}else if($nbSetEq1<=1 && $nbSetEq2==3){
				$arrayPoints[$row['id2']] += 3;
				$arrayMatchsGagnes[$row['id2']] += 1;
                                $arrayMatchsPerdu[$row['id1']] += 1;
                                $arrayMatchs0_3[$row['id1']] += 1;
                                $arrayMatchs1_3[$row['id1']] += 1;
                                $arrayMatchs3_0[$row['id2']] += 1;
                                $arrayMatchs3_1[$row['id2']] += 1;
			}
			
			//calcul de la difference de sets
			$arrayDiffSets[$row['id1']] += $nbSetEq1 - $nbSetEq2;
			$arrayDiffSets[$row['id2']] += $nbSetEq2 - $nbSetEq1;
			
			//calcul de la difference de points
			$arrayDiffPoints[$row['id1']] += $row['Score_Set1_Dom'] + $row['Score_Set2_Dom'] +$row['Score_Set3_Dom'] +$row['Score_Set4_Dom'] +$row['Score_Set5_Dom'] 
			- $row['Score_Set1_Ext'] -$row['Score_Set2_Ext'] -$row['Score_Set3_Ext'] -$row['Score_Set4_Ext'] -$row['Score_Set5_Ext'];
			$arrayDiffPoints[$row['id2']] += 0-$row['Score_Set1_Dom'] - $row['Score_Set2_Dom'] -$row['Score_Set3_Dom'] -$row['Score_Set4_Dom'] -$row['Score_Set5_Dom'] 
			+ $row['Score_Set1_Ext'] +$row['Score_Set2_Ext'] +$row['Score_Set3_Ext'] +$row['Score_Set4_Ext'] +$row['Score_Set5_Ext'];
			
			
        }
		//svg du tab des points
		$tabPoints = array();
		$tabPoints=$arrayPoints;
		//fusion des tableaux
		foreach ($arrayDiffSets as $key => $val) {
			$tot=5000+$val;
			$arrayPoints[$key] = $arrayPoints[$key]."/".$tot;
		}
		foreach ($arrayDiffPoints as $key => $val) {
			$tot=50000+$val;
			$arrayPoints[$key] = $arrayPoints[$key]."/".$tot;
		}

		//on va maintenant trier le tableau par points
		arsort($arrayPoints);
		//Affichage du classemement par points
		echo "Classement par points<br><br>";
		foreach ($arrayPoints as $key => $val) {
			echo "Equipe $key avec $val points <br>";
			echo "diff sets".$arrayDiffSets[$key]."<br><br>";
			
		}
          $reqNomEquipe="SELECT Libelle_Equipe as NomEquipe FROM volley_equipe WHERE Id_Equipe='".$key."'";
          $result = $connexion->query($reqNomEquipe);
          

	   /*
		//on va maintenant trier le tableau par difference de sets
		arsort($arrayDiffSets);
		//Affichage du classemement par sets
		echo "<br> <br> Difference de sets";
		foreach ($arrayDiffSets as $key => $val) {
			echo "Equipe $key avec $val diffSets <br>";
		}
		//on va maintenant trier le tableau par difference de sets
		arsort($arrayDiffPoints);
		//Affichage du classemement par points
		echo "<br> <br> Difference de points";
		foreach ($arrayDiffPoints as $key => $val) {
			echo "Equipe $key avec $val diffPoints <br>";
		}
			
		*/
/*
		//Affichage du classemement final
		echo "<br> <br> Classement a afficher";
		foreach ($data as $key => $val) {
			echo "Equipe $key avec $val  <br>";
		}
	*/	
                            
 ?>
    <div class="tableau">
        <table cellspacing="0">
        <tr>
            <td></td>
            <td>Equipe</td>
            <td>Points</td>
            <td>Jou.</td>
            <td>Gag.</td>
            <td>Per.</td>
            <td>3-0</td>
            <td>3-1</td>
            <td>3-2</td>
            <td>2-3</td>
            <td>1-3</td>
            <td>0-3</td>
            <td>Set.P</td>
            <td>Set.C</td>
            <td>Coeff.S</td>
            <td>Pts.P</td>
            <td>Pts.C</td>
            <td>Coeff.P</td>
        </tr>
        
        
            <?php 
			$place=1;
            foreach ($arrayPoints as $key => $val) {
				
				$reqNomEquipe="SELECT Libelle_Equipe as NomEquipe FROM volley_equipe WHERE Id_Equipe='".$key."'";
				$result = $connexion->query($reqNomEquipe);
				while ($row=$result->fetch(PDO::FETCH_ASSOC))
				{
					echo "<tr>";
					echo "<td>".$place."</td>";
					echo "<td>".$row['NomEquipe']. "</td>";
					echo "<td>" . $tabPoints[$key]."</td>";
					echo "<td>".$arrayMatchsJoues[$key]."</td>";
					echo "<td>".$arrayMatchsGagnes[$key]."</td>";
					echo "<td>".$arrayMatchsPerdu[$key]."</td>";
					echo "<td>".$arrayMatchs3_0[$key]."</td>";
					echo "<td>".$arrayMatchs3_1[$key]."</td>";
					echo "<td>".$arrayMatchs3_2[$key]."</td>";
					echo "<td>".$arrayMatchs2_3[$key]."</td>";
					echo "<td>".$arrayMatchs1_3[$key]."</td>";
					echo "<td>".$arrayMatchs0_3[$key]."</td>";
					echo "<td></td>";
					echo "<td>" . $arrayDiffSets[$key]."</td>";
					echo "<td></td>";
					echo "<td></td>";
					echo "<td>" . $arrayDiffPoints[$key]."</td>";
                                        echo "<td></td>";
					echo "</tr>";
				}
				$place++;
			}
       
		  
                    //echo "<td>""</td>";
                
            ?>
        
        </table>
      
        
    </div>
    <?php } ?>
    <br>
</center>